/**
 *
 */
package com.angel.io.log;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import com.angel.architecture.persistence.base.PersistentObject;
import com.angel.io.processors.commands.imports.ImportFileProcessorCommand;
import com.angel.io.processors.commands.imports.ImportRowProcessorCommand;

/**
 * @author William
 *
 */
@Entity
public class ProcessorLog extends PersistentObject{

	private static final long serialVersionUID = -867369686314545158L;

	@Transient
	private ImportRowProcessorCommand rowProcessorCommand;
	@Transient
	private ImportFileProcessorCommand fileProcessorCommand;
	private Date startProcess;
	private Date endProcess;
	private long numberPage;
	@OneToMany(cascade = CascadeType.ALL)
	private List<RowMessageLog> rowMessageLogs;

	public ProcessorLog(String name){
		super();
		this.rowMessageLogs = new ArrayList<RowMessageLog>();
		this.startProcess = new Date();
	}

	public ProcessorLog(String name, ImportFileProcessorCommand fileProcessorCommand, ImportRowProcessorCommand rowProcessorCommand){
		this(name);
		this.rowProcessorCommand = rowProcessorCommand;
		this.fileProcessorCommand = fileProcessorCommand;
	}

	/**
	 * @return the rowProcessorCommand
	 */
	public ImportRowProcessorCommand getRowProcessorCommand() {
		return rowProcessorCommand;
	}

	/**
	 * @param rowProcessorCommand the rowProcessorCommand to set
	 */
	public void setRowProcessorCommand(ImportRowProcessorCommand rowProcessorCommand) {
		this.rowProcessorCommand = rowProcessorCommand;
	}

	/**
	 * @return the fileProcessorCommand
	 */
	public ImportFileProcessorCommand getFileProcessorCommand() {
		return fileProcessorCommand;
	}

	/**
	 * @param fileProcessorCommand the fileProcessorCommand to set
	 */
	public void setFileProcessorCommand(ImportFileProcessorCommand fileProcessorCommand) {
		this.fileProcessorCommand = fileProcessorCommand;
	}

	/**
	 * @return the rowMessageLogs
	 */
	public List<RowMessageLog> getRowMessageLogs() {
		return rowMessageLogs;
	}

	/**
	 * @param rowMessageLogs the rowMessageLogs to set
	 */
	public void setRowMessageLogs(List<RowMessageLog> rowMessageLogs) {
		this.rowMessageLogs = rowMessageLogs;
	}

	public boolean addRowMessageLog(RowMessageLog rowMessageLog){
		return this.rowMessageLogs.add(rowMessageLog);
	}

	public boolean addFailRowMessageLog(Long rowNumber, String comment){
		RowMessageLog rowMessageLog = new RowMessageLog(RowState.FAIL, rowNumber, comment);
		return this.addRowMessageLog(rowMessageLog);
	}

	public boolean addProcessedRowMessageLog(Long rowNumber, String comment){
		RowMessageLog rowMessageLog = new RowMessageLog(RowState.PROCESSED, rowNumber, comment);
		return this.addRowMessageLog(rowMessageLog);
	}

	public boolean addRowMessageLog(RowState rowState, Long rowNumber, String comment){
		RowMessageLog rowMessageLog = new RowMessageLog(rowState, rowNumber, comment);
		return this.addRowMessageLog(rowMessageLog);
	}

	/**
	 * @return the startProcess
	 */
	public Date getStartProcess() {
		return startProcess;
	}

	/**
	 * @param startProcess the startProcess to set
	 */
	public void setStartProcess(Date startProcess) {
		this.startProcess = startProcess;
	}

	/**
	 * @return the endProcess
	 */
	public Date getEndProcess() {
		return endProcess;
	}

	/**
	 * @param endProcess the endProcess to set
	 */
	public void setEndProcess(Date endProcess) {
		this.endProcess = endProcess;
	}

	/**
	 * @return the numberPage
	 */
	public long getNumberPage() {
		return numberPage;
	}

	/**
	 * @param numberPage the numberPage to set
	 */
	public void setNumberPage(long numberPage) {
		this.numberPage = numberPage;
	}

	public long countRowMessages() {
		return this.getRowMessageLogs().size();
	}
}
